home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / VideoStreamV1.0 / Source / MPEGView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.1 KB  |  109 lines

  1. /*
  2.  * Copyright 1994, Black Adder Research, Inc.  This source code may be
  3.  * redistributed and modified with one restriction - do not claim that 
  4.  * you wrote it.
  5.  *
  6.  * Black Adder Research, Inc.
  7.  * 730 Norell Ave. North
  8.  * Stillwater, MN  55082
  9.  * 
  10.  */
  11.  
  12.  
  13. #import "Controller.h"
  14. #import "MPEGView.h"
  15. #import <appkit/appkit.h>
  16.  
  17. @implementation MPEGView : View
  18.  
  19. - initSize: (const NXSize *) aSize
  20. {
  21.     NXRect imageRect;
  22.     id retVal = nil;
  23.  
  24.     NXSetRect(&imageRect, 0.0, 0.0, aSize->width, aSize->height);
  25.     [super initFrame:&imageRect];
  26.     strcpy(mpgFile, "");
  27.     if((theData = (unsigned char *)malloc(3 * (int) (aSize->width * aSize->height))) != NULL) {
  28.         if((theBitmap = [[NXBitmapImageRep alloc] initData:theData
  29.             pixelsWide: (int) aSize->width
  30.             pixelsHigh: (int) aSize->height
  31.             bitsPerSample: 8
  32.             samplesPerPixel:3
  33.             hasAlpha:NO
  34.             isPlanar:NO
  35.             colorSpace: NX_RGBColorSpace
  36.             bytesPerRow:0
  37.             bitsPerPixel:0]) != nil) {
  38.     
  39.                 retVal = self;
  40.         }
  41.     }
  42.     return(retVal);
  43. }
  44.  
  45.  
  46. - runFromFile: (char *) mpegFile
  47. {
  48.     strcpy(mpgFile, mpegFile);
  49.     [self runAgain];
  50.     return(self);
  51. }
  52.  
  53.  
  54. - runAgain
  55. {
  56.     char command[256];
  57.     int numPix, numFrPix;
  58.     BOOL shouldLoop = YES;
  59.     FILE *ifp;
  60.  
  61.     if (strcmp(mpgFile, "") != 0) {
  62.         numPix = 3 * [theBitmap pixelsWide] * [theBitmap pixelsHigh];
  63.         numFrPix = numPix + 3 * sizeof(int); /* for frame number, width, and height */
  64.         sprintf(command, "%s/mpegDecode %s", [[NXBundle mainBundle] directory], mpgFile);
  65.         if ((ifp = popen(command, "r")) != NULL) {
  66.             [self lockFocus];
  67.             while(shouldLoop) {
  68.                 if(NXUserAborted()) {
  69.                     NXResetUserAbort();
  70.                     if(NX_ALERTDEFAULT == NXRunAlertPanel("MPEGStream", 
  71.                             "Stop Playback?", "Yes", "No", NULL) ) {
  72.                         shouldLoop = NO;
  73.                         break;
  74.                     }
  75.                 }
  76.                 fread(theData, 1, 3*sizeof(int), ifp); /* skip frame #, width, height */
  77.                 if(fread(theData, 1, numPix, ifp) != numPix) {
  78.                     shouldLoop = NO;
  79.                     break;
  80.                 }
  81.                 else {
  82.                     [theBitmap draw];
  83.                     NXPing();
  84.                 }
  85.             }
  86.             [self unlockFocus];
  87.             pclose(ifp);
  88.         }
  89.     }
  90.     return(self);
  91. }
  92.  
  93.  
  94. - drawSelf:(const NXRect *)rects :(int)rectCount
  95. {
  96.     [theBitmap draw];
  97.     return self;
  98. }
  99.  
  100.  
  101. - free
  102. {
  103.     free(theData);
  104.     [theBitmap free];
  105.     return [super free];
  106. }
  107.  
  108. @end
  109.